﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using eSpaceLib;

namespace com.huawei.ebg.esdk.ucsdk.client.control
{
    public static class CtrlBusiness
    {
        private static ECSEspaceAppClass _myApp;
        internal static ECSEspaceAppClass MyApp
        {
            get
            {
                if (_myApp == null)
                {
                    _myApp = new ECSEspaceAppClass();
                }
                return _myApp;
            }
            set
            {
                _myApp = value;
            }
        }

        private static ECSMainWindow myESpace;

        # region 拨号盘

        public static void ShowDialPad(int x, int y)
        {
            try
            {
                //启动eSpace
                MyApp = new ECSEspaceAppClass();
                //如果eSpace未登陆则弹出eSpace登陆界面
                myESpace = MyApp.GetMainWindow();
                if (myESpace == null)
                {
                    return;
                }
                myESpace.ShowDialPad(x, y);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        # endregion

        # region 直接呼叫

        public static void DirectCall(string ucAccount, string calledNumber)
        {
            //启动eSpace
            MyApp = new ECSEspaceAppClass();
            //如果eSpace未登陆则弹出eSpace登陆界面
            myESpace = MyApp.GetMainWindow();
            if (myESpace == null)
            {
                return;
            }

            Dialing(ucAccount, calledNumber);
        }

        private static void Dialing(string ucAccount, string calledNumber)
        {
            try
            {
                if (Utilities.ItemExisted(ucAccount))
                {
                    //查询UC帐号是否存在
                    ECSContactInfo accountInfo = MyApp.GetContactMgr().GetContactInfo(ucAccount);
                    if (accountInfo == null)
                    {
                        if (Utilities.ItemExisted(calledNumber))
                        {
                            //UC帐号不存在，以设置的号码进行呼叫
                            MakeCall(calledNumber);
                        }
                    }
                    else
                    {
                        //以UC帐号进行呼叫
                        string uri = accountInfo.GetUcUri();
                        MakeCall(uri, calledNumber);
                    }
                }
                //以设置的号码进行呼叫
                else if (Utilities.ItemExisted(calledNumber))
                {
                    MakeCall(calledNumber);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private static void MakeCall(string calledNumber)
        {
            try
            {
                MakeCall(string.Empty, calledNumber);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private static void MakeCall(string uri, string calledNumber)
        {
            try
            {
                myESpace.MakeCall(uri, calledNumber);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        # endregion

        # region 选择呼叫

        public static ArrayList GetPhoneNbr(string ucAccount)
        {
            try
            {
                //启动eSpace
                MyApp = new ECSEspaceAppClass();
                //如果eSpace未登陆则弹出eSpace登陆界面
                myESpace = MyApp.GetMainWindow();
                if (myESpace == null)
                {
                    return null;
                }
                ArrayList myList = new ArrayList();
                if (Utilities.ItemExisted(ucAccount))
                {
                    //查询UC帐号是否存在
                    ECSContactInfo accountInfo = MyApp.GetContactMgr().GetContactInfo(ucAccount);
                    if (accountInfo != null)
                    {
                        //查询UC帐号对应的号码
                        string ucNumber = accountInfo.GetUcUri();
                        //如果UCUri存在，则显示UC帐号作为UC号码
                        if (ucNumber != string.Empty)
                        { ucNumber = ucAccount; }
                        string mobile = accountInfo.GetMobile();
                        string homePhone = accountInfo.GetHomePhone();
                        string officePhone = accountInfo.GetOfficePhone();
                        string otherPhone = accountInfo.GetOtherPhone();

                        //用ArrayList顺序存储号码的键值
                        myList.Insert(0, ucNumber);
                        myList.Insert(1, mobile);
                        myList.Insert(2, homePhone);
                        myList.Insert(3, officePhone);
                        myList.Insert(4, otherPhone);
                    }
                }
                return myList;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        # endregion

        # region 即时消息会议

        public static void CreateIMMeeting()
        {
            try
            {
                //启动eSpace
                MyApp = new ECSEspaceAppClass();
                //如果eSpace未登陆则弹出eSpace登陆界面
                myESpace = MyApp.GetMainWindow();
                if (myESpace == null)
                {
                    return;
                }
                myESpace.ShowCreateIMMeeting();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        # endregion

        # region 语音会议

        public static void CreateMMMeeting()
        {
            try
            {
                //启动eSpace
                MyApp = new ECSEspaceAppClass();
                //如果eSpace未登陆则弹出eSpace登陆界面
                myESpace = MyApp.GetMainWindow();
                if (myESpace == null)
                {
                    return;
                }
                myESpace.ShowCreateMMMeeting();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        # endregion

        # region 会议管理

        public static void ShowMeetingMgr()
        {
            try
            {
                //启动eSpace
                MyApp = new ECSEspaceAppClass();
                //如果eSpace未登陆则弹出eSpace登陆界面
                myESpace = MyApp.GetMainWindow();
                if (myESpace == null)
                {
                    return;
                }
                myESpace.ShowMeetingMgr();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        # endregion

        # region 即时消息

        public static void ShowIMChat(string ucAccount)
        {
            try
            {
                //启动eSpace
                MyApp = new ECSEspaceAppClass();
                //如果eSpace未登陆则弹出eSpace登陆界面
                myESpace = MyApp.GetMainWindow();
                if (myESpace == null)
                {
                    return;
                }
                //查询UC帐号是否存在
                ECSContactInfo accountInfo = MyApp.GetContactMgr().GetContactInfo(ucAccount);
                if (accountInfo == null)
                {
                    return;
                }
                if (ucAccount != null && ucAccount != string.Empty)
                {
                    myESpace.ShowIMChat(ucAccount);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        # endregion

        # region 状态呈现

        public static int GetStatus(string ucAccount)
        {
            try
            {
                //启动eSpace
                MyApp = new ECSEspaceAppClass();
                //获取用户状态
                int myStatus = 0;
                ECSContactMgr accountMgr = MyApp.GetContactMgr();
                if (accountMgr != null)
                {
                    ECSContactInfo accountInfo = accountMgr.GetContactInfo(ucAccount);
                    if (accountInfo != null)
                    {
                        myStatus = accountInfo.GetStatus();
                        if (myStatus != 6)
                        {
                            //Type为0表示订阅。Type为1表示取消订阅
                            accountMgr.SubscribeContactStatus(ucAccount, 0);
                        }
                    }
                }
                return myStatus;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        # endregion

        # region 公用方法

        public static string GetLoginUser()
        {
            try
            {
                ECSContactInfo myContactInfo = MyApp.GetSelfInfo();
                if (myContactInfo != null)
                {
                    return myContactInfo.GetUcAccount();
                }
                else
                {
                    return string.Empty;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        # endregion
        #region 状态订阅
        //Type为0表示订阅。Type为1表示取消订阅
        public static void SubScribe(string account, int subType)
        {
            try
            {
                ECSEspaceAppClass espaceApp = new ECSEspaceAppClass();
                ECSContactMgr contactMgr = espaceApp.GetContactMgr();
                if (contactMgr != null)
                {
                     contactMgr.SubscribeContactStatus(account, subType);
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            
        }
        #endregion
    }
}
